package com.funsport.controller.mobile;

import com.funsport.common.Result;
import com.funsport.security.SecurityUtils;
import com.funsport.service.IUserStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 移动端统计数据控制器
 *
 * @author Fun-Sport Team
 */
@Tag(name = "移动端统计数据", description = "移动端统计数据相关接口")
@RestController
@RequestMapping("/mobile/stats")
@Slf4j
public class MobileStatsController {
    
    @Autowired
    private IUserStatsService userStatsService;

    @Operation(summary = "获取用户统计数据")
    @GetMapping("/user")
    public Result<Map<String, Object>> getUserStats() {
        try {
            Long userId = SecurityUtils.getCurrentUserIdOrNull();
            Map<String, Object> stats = userStatsService.getDashboardStats(userId);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取用户统计数据失败", e);
            return Result.error("获取统计数据失败");
        }
    }

    @Operation(summary = "获取首页统计数据")
    @GetMapping("/dashboard")
    public Result<Map<String, Object>> getDashboardStats() {
        try {
            Long userId = SecurityUtils.getCurrentUserIdOrNull();
            Map<String, Object> stats = userStatsService.getDashboardStats(userId);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取首页统计数据失败", e);
            return Result.error("获取统计数据失败");
        }
    }

    @Operation(summary = "获取用户积分信息")
    @GetMapping("/points")
    public Result<Map<String, Object>> getUserPoints() {
        try {
            Long userId = SecurityUtils.getCurrentUserIdOrNull();
            Map<String, Object> points = userStatsService.getUserPoints(userId);
            return Result.success(points);
        } catch (Exception e) {
            log.error("获取用户积分信息失败", e);
            return Result.error("获取积分信息失败");
        }
    }

    @Operation(summary = "获取用户本月活动统计")
    @GetMapping("/monthly-events")
    public Result<Map<String, Object>> getMonthlyEvents() {
        try {
            Long userId = SecurityUtils.getCurrentUserIdOrNull();
            if (userId == null) {
                return Result.error("用户未登录");
            }
            
            Map<String, Object> monthlyStats = userStatsService.getMonthlyEventStats(userId);
            return Result.success(monthlyStats);
        } catch (Exception e) {
            log.error("获取用户本月活动统计失败", e);
            return Result.error("获取活动统计失败");
        }
    }
    
    @Operation(summary = "获取积分记录列表")
    @GetMapping("/points/records")
    public Result<java.util.List<com.funsport.entity.PointRecord>> getPointsRecords(
            @org.springframework.web.bind.annotation.RequestParam(required = false) String type) {
        try {
            Long userId = SecurityUtils.getCurrentUserIdOrNull();
            if (userId == null) {
                return Result.error("用户未登录");
            }
            
            java.util.List<com.funsport.entity.PointRecord> records = userStatsService.getPointsRecords(userId, type);
            return Result.success(records);
        } catch (Exception e) {
            log.error("获取积分记录失败", e);
            return Result.error("获取积分记录失败");
        }
    }
}
